#include "focusitem.h"
#include <QCursor>
#include <QBrush>
#include <QPainter>
#include "math.h"

FocusItem::FocusItem(FocusItem::PosInHost pos,
                     FocusItem::FocusType type,
                     QGraphicsItem *parent)
    : QGraphicsRectItem(0, 0, 6, 6, parent),
      m_posInHost(pos), m_type(type)
{
    setAcceptsHoverEvents(true);
    setVisible(false);
    setBrush(QBrush(Qt::red));

    m_size = 6;
    m_degree = 0;
    locateInHost();
}

FocusItem::FocusItem(qreal len,
                     FocusItem::PosInHost pos,
                     FocusItem::FocusType type,
                     QGraphicsItem *parent)
    : QGraphicsRectItem(0, 0, len, len, parent),
      m_posInHost(pos), m_type(type)
{
    setAcceptsHoverEvents(true);
    setVisible(false);
    setBrush(QBrush(Qt::red));

    m_size = len;
    m_degree = 0;
    locateInHost();
}

void FocusItem::locateInHost()
{
    const QRectF parentRect = this->parentItem()->boundingRect();
    qreal width = parentRect.width();
    qreal height = parentRect.height();
    qreal x = 0, y = 0;

    qreal tempWidth = width;
    qreal tempHeight = height;
    if (m_type == ROTATE)
    {
        tempWidth = m_width;
        tempHeight = m_height;
    }

    switch(m_posInHost)
    {
    case NORTH_MIDDLE:
        x = tempWidth / 2 - m_size / 2;
        y = 0;
        break;
    case SOUTH_MIDDLE:
        x = tempWidth / 2 - m_size / 2;
        y = tempHeight - m_size;
        break;
    case EAST_MIDDLE:
        x = tempWidth - m_size;
        y = tempHeight / 2 - m_size / 2;
        break;
    case WEST_MIDDLE:
        x = 0;
        y = tempHeight / 2 - m_size / 2;
        break;
    case NORTH_WEST:
        x = 0;
        y = 0;
        break;
    case SOUTH_EAST:
        x = tempWidth - m_size;
        y = tempHeight - m_size;
        break;
    case NORTH_EAST:
        x = tempWidth - m_size;
        y = 0;
        break;
    case SOUTH_WEST:
        x = 0;
        y = tempHeight - m_size;
        break;
    default:
        break;
    }
    qreal temp = -m_degree * 180 / PI;
    setRotation(temp);

    qreal tempX = x - tempWidth / 2;
    qreal tempY = tempHeight / 2 - y;

    qreal realX = tempX * cos(m_degree) - tempY * sin(m_degree);
    qreal realY = tempY * cos(m_degree) + tempX * sin(m_degree);
    realX = realX + width / 2;
    realY = height / 2 - realY;
    setPos(realX,realY);
}

void FocusItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
    if (m_type == RESIZE)
    {
        switch(m_posInHost)
        {
        case NORTH_MIDDLE:
            setCursor(Qt::SizeVerCursor);
            break;
        case SOUTH_MIDDLE:
            setCursor(Qt::SizeVerCursor);
            break;
        case EAST_MIDDLE:
            setCursor(Qt::SizeHorCursor);
            break;
        case WEST_MIDDLE:
            setCursor(Qt::SizeHorCursor);
            break;
        case NORTH_WEST:
            setCursor(Qt::SizeFDiagCursor);
            break;
        case SOUTH_EAST:
            setCursor(Qt::SizeFDiagCursor);
            break;
        case NORTH_EAST:
            setCursor(Qt::SizeBDiagCursor);
            break;
        case SOUTH_WEST:
            setCursor(Qt::SizeBDiagCursor);
            break;
        default:
            break;
        }
    }
    else if (m_type == ROTATE)
    {
        QPixmap pixmap(":/cursor/rotatecursor.png");
        QCursor m_cursor(pixmap);
        setCursor(m_cursor);
    }
    QGraphicsRectItem::hoverEnterEvent(event);
}

void FocusItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
    QGraphicsRectItem::hoverLeaveEvent(event);
}
